const utils = require('./index');
// 添加统一的返回结果方法
module.exports = async function (req, res, next) {
	res.sendResult = async function (obj) {
		var fmt = req.query.fmt ? req.query.fmt : "rest";
		if (fmt == "rest") {
			res.json(utils.tools.lineToHump({
				"data": obj.data,
				"meta": {
					"msg": res.__(obj.message || '操作成功！'),
					"status": obj.code
				}
			}))
		}
	};
	//自定义格式
	res.sendResultAto = async function (data, code, message = '操作成功！') {
		var fmt = req.query.fmt ? req.query.fmt : "rest";
		if (fmt == "rest") {
			res.json(utils.tools.lineToHump({
				"data": data,
				"meta": {
					"msg": res.__(message),
					"status": code
				}
			}))

		}
	};
	next();
}

